package com.heima.kafka.boot.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 消费者发送消息的类
 */
@RestController
@RequestMapping("/send")
public class ProducerController {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @GetMapping
    public String sendMessage(){
        Map map = new HashMap();
        map.put("name","胡汉三");
        kafkaTemplate.send("boot-topic", JSON.toJSONString(map));
        return "发送成功";
    }
}


















